2 * JavaScript for Special:Preferences
6 var $preftoc
, $preferences
, $fieldsets
, labelFunc
, previousTab
,
7 $tzSelect
, $tzTextbox
, $localtimeHolder
, servertime
, allowCloseWindow
,
8 convertmessagebox
= require( 'mediawiki.notification.convertmessagebox' );
10 labelFunc = function () {
11 return this.id
.replace( /^mw-prefsection/g, 'preftab' );
14 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
15 $preftoc
= $( '#preftoc' );
16 $preferences
= $( '#preferences' );
18 $fieldsets
= $preferences
.children( 'fieldset' )
21 'aria-labelledby': labelFunc
23 $fieldsets
.not( '#mw-prefsection-personal' )
25 .attr( 'aria-hidden', 'true' );
27 // T115692: The following is kept for backwards compatibility with older skins
28 $preferences
.addClass( 'jsprefs' );
29 $fieldsets
.addClass( 'prefsection' );
30 $fieldsets
.children( 'legend' ).addClass( 'mainLegend' );
32 // Make sure the accessibility tip is selectable so that screen reader users take notice,
33 // but hide it per default to reduce interface clutter. Also make sure it becomes visible
34 // when selected. Similar to jquery.mw-jump
35 $( '<div>' ).addClass( 'mw-navigation-hint' )
36 .text( mw
.msg( 'prefs-tabs-navigation-hint' ) )
37 .attr( 'tabIndex', 0 )
38 .on( 'focus blur', function ( e
) {
39 if ( e
.type
=== 'blur' || e
.type
=== 'focusout' ) {
40 $( this ).css( 'height', '0' );
42 $( this ).css( 'height', 'auto' );
44 } ).insertBefore( $preftoc
);
47 * It uses document.getElementById for security reasons (HTML injections in $()).
50 * @param {string} name the name of a tab without the prefix ("mw-prefsection-")
51 * @param {string} [mode] A hash will be set according to the current
52 * open section. Set mode 'noHash' to surpress this.
54 function switchPrefTab( name
, mode
) {
56 // Handle hash manually to prevent jumping,
57 // therefore save and restore scrollTop to prevent jumping.
58 scrollTop
= $( window
).scrollTop();
59 if ( mode
!== 'noHash' ) {
60 location
.hash
= '#mw-prefsection-' + name
;
62 $( window
).scrollTop( scrollTop
);
64 $preftoc
.find( 'li' ).removeClass( 'selected' )
67 'aria-selected': 'false'
70 $tab
= $( document
.getElementById( 'preftab-' + name
) );
74 'aria-selected': 'true'
76 .parent().addClass( 'selected' );
78 $preferences
.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
79 $( document
.getElementById( 'mw-prefsection-' + name
) ).show().attr( 'aria-hidden', 'false' );
83 // Check for successbox to replace with notifications
86 // Enable keyboard users to use left and right keys to switch tabs
87 $preftoc
.on( 'keydown', function ( event
) {
92 if ( event
.keyCode
=== keyLeft
) {
93 $el
= $( '#preftoc li.selected' ).prev().find( 'a' );
94 } else if ( event
.keyCode
=== keyRight
) {
95 $el
= $( '#preftoc li.selected' ).next().find( 'a' );
99 if ( $el
.length
> 0 ) {
100 switchPrefTab( $el
.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
104 // Jump to correct section as indicated by the hash.
105 // This function is called onload and onhashchange.
106 function detectHash() {
107 var hash
= location
.hash
,
108 matchedElement
, parentSection
;
109 if ( hash
.match( /^#mw-prefsection-[\w-]+/ ) ) {
110 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
111 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
112 } else if ( hash
.match( /^#mw-[\w-]+/ ) ) {
113 matchedElement
= document
.getElementById( hash
.slice( 1 ) );
114 parentSection
= $( matchedElement
).closest( '.prefsection' );
115 if ( parentSection
.length
) {
116 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
117 // Switch to proper tab and scroll to selected item.
118 switchPrefTab( parentSection
.attr( 'id' ).replace( 'mw-prefsection-', '' ), 'noHash' );
119 matchedElement
.scrollIntoView();
124 // In browsers that support the onhashchange event we will not bind click
125 // handlers and instead let the browser do the default behavior (clicking the
126 // <a href="#.."> will naturally set the hash, handled by onhashchange.
127 // But other things that change the hash will also be caught (e.g. using
128 // the Back and Forward browser navigation).
129 // Note the special check for IE "compatibility" mode.
130 if ( 'onhashchange' in window
&&
131 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
133 $( window
).on( 'hashchange', function () {
134 var hash
= location
.hash
;
135 if ( hash
.match( /^#mw-[\w-]+/ ) ) {
137 } else if ( hash
=== '' ) {
138 switchPrefTab( 'personal', 'noHash' );
141 // Run the function immediately to select the proper tab on startup.
142 .trigger( 'hashchange' );
143 // In older browsers we'll bind a click handler as fallback.
144 // We must not have onhashchange *and* the click handlers, otherwise
145 // the click handler calls switchPrefTab() which sets the hash value,
146 // which triggers onhashchange and calls switchPrefTab() again.
148 $preftoc
.on( 'click', 'li a', function ( e
) {
149 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
152 // If we've reloaded the page or followed an open-in-new-window,
153 // make the selected tab visible.
157 // Timezone functions.
158 // Guesses Timezone from browser and updates fields onchange.
160 $tzSelect
= $( '#mw-input-wptimecorrection' );
161 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
162 $localtimeHolder
= $( '#wpLocalTime' );
163 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
165 function minutesToHours( min
) {
166 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
167 tzMin
= Math
.abs( min
) % 60,
168 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
169 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
173 function hoursToMinutes( hour
) {
175 arr
= hour
.split( ':' );
177 arr
[ 0 ] = parseInt( arr
[ 0 ], 10 );
179 if ( arr
.length
=== 1 ) {
180 // Specification is of the form [-]XX
181 minutes
= arr
[ 0 ] * 60;
183 // Specification is of the form [-]XX:XX
184 minutes
= Math
.abs( arr
[ 0 ] ) * 60 + parseInt( arr
[ 1 ], 10 );
185 if ( arr
[ 0 ] < 0 ) {
189 // Gracefully handle non-numbers.
190 if ( isNaN( minutes
) ) {
197 function updateTimezoneSelection() {
198 var minuteDiff
, localTime
,
199 type
= $tzSelect
.val();
201 if ( type
=== 'other' ) {
202 // User specified time zone manually in <input>
203 // Grab data from the textbox, parse it.
204 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
206 // Time zone not manually specified by user
207 if ( type
=== 'guess' ) {
208 // Get browser timezone & fill it in
209 minuteDiff
= -( new Date().getTimezoneOffset() );
210 $tzTextbox
.val( minutesToHours( minuteDiff
) );
211 $tzSelect
.val( 'other' );
212 $tzTextbox
.prop( 'disabled', false );
214 // Grab data from the $tzSelect value
215 minuteDiff
= parseInt( type
.split( '|' )[ 1 ], 10 ) || 0;
216 $tzTextbox
.val( minutesToHours( minuteDiff
) );
219 // Set defaultValue prop on the generated box so we don't trigger the
220 // unsaved preferences check
221 $tzTextbox
.prop( 'defaultValue', $tzTextbox
.val() );
224 // Determine local time from server time and minutes difference, for display.
225 localTime
= servertime
+ minuteDiff
;
227 // Bring time within the [0,1440) range.
228 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
230 $localtimeHolder
.text( mw
.language
.convertNumber( minutesToHours( localTime
) ) );
233 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
234 $tzSelect
.change( updateTimezoneSelection
);
235 $tzTextbox
.blur( updateTimezoneSelection
);
236 updateTimezoneSelection();
239 // Restore the active tab after saving the preferences
240 previousTab
= mw
.storage
.session
.get( 'mwpreferences-prevTab' );
242 switchPrefTab( previousTab
, 'noHash' );
243 // Deleting the key, the tab states should be reset until we press Save
244 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
247 $( '#mw-prefs-form' ).on( 'submit', function () {
248 var value
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
249 mw
.storage
.session
.set( 'mwpreferences-prevTab', value
);
252 // Check if all of the form values are unchanged
253 function isPrefsChanged() {
254 var inputs
= $( '#mw-prefs-form :input[name]' ),
255 input
, $input
, inputType
,
259 for ( index
= 0; index
< inputs
.length
; index
++ ) {
260 input
= inputs
[ index
];
263 // Different types of inputs have different methods for accessing defaults
264 if ( $input
.is( 'select' ) ) {
265 // <select> has the property defaultSelected for each option
266 for ( optIndex
= 0; optIndex
< input
.options
.length
; optIndex
++ ) {
267 opt
= input
.options
[ optIndex
];
268 if ( opt
.selected
!== opt
.defaultSelected
) {
272 } else if ( $input
.is( 'input' ) ) { // <input> has defaultValue or defaultChecked
273 inputType
= input
.type
;
274 if ( inputType
=== 'radio' || inputType
=== 'checkbox' ) {
275 if ( input
.checked
!== input
.defaultChecked
) {
278 } else if ( input
.value
!== input
.defaultValue
) {
287 // Disable the button to save preferences unless preferences have changed
288 // Check if preferences have been changed before JS has finished loading
289 if ( !isPrefsChanged() ) {
290 $( '#prefcontrol' ).prop( 'disabled', true );
291 $( '#preferences > fieldset' ).one( 'change keydown mousedown', function () {
292 $( '#prefcontrol' ).prop( 'disabled', false );
296 // Set up a message to notify users if they try to leave the page without
298 allowCloseWindow
= mw
.confirmCloseWindow( {
299 test
: isPrefsChanged
,
300 message
: mw
.msg( 'prefswarning-warning', mw
.msg( 'saveprefs' ) ),
301 namespace: 'prefswarning'
303 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow
, 'release' ) );
304 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow
, 'release' ) );
306 }( mediaWiki
, jQuery
) );